package Visao;

import Modelo.Hospede;
import Modelo.Reserva;
import RMI.ServicosCliente;
import RMI.ServicosIF;
import Util.Utils;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Pablo Veinberg <pveinberg@gmail.com>
 */
public class FormCheckOut extends javax.swing.JDialog {

    private Reserva reserva;
    private double totalHospedagem;
    private double consumos = 0;
    private double saldo = 0;
    private double total = 0;

    public FormCheckOut(Reserva r) {
        initComponents();
        setLocationRelativeTo(null);

        if (r == null) {
            JOptionPane.showMessageDialog(null, "Selecione uma reserva.");
            this.setVisible(false);
        }

        this.reserva = r;
        this.totalHospedagem = this.reserva.getTotalHospedagem();
        this.saldoInput.setText(String.valueOf(this.totalHospedagem));
    }

    public Reserva getReserva() {
        return reserva;
    }

    public void setReserva(Reserva reserva) {
        this.reserva = reserva;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        confirma = new javax.swing.JButton();
        cancela = new javax.swing.JButton();
        checkOutTitulo = new javax.swing.JLabel();
        consumoLabel = new javax.swing.JLabel();
        consumoInput = new javax.swing.JTextField();
        saldoInput = new javax.swing.JTextField();
        saldoLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        totalPendente = new javax.swing.JTextField();
        calcular = new javax.swing.JButton();
        labelPagamento = new javax.swing.JLabel();
        inputPagamento = new javax.swing.JTextField();

        setTitle("Cadastro de Quartos");
        setModal(true);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        confirma.setText("Confirma");
        confirma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmaActionPerformed(evt);
            }
        });

        cancela.setText("Cancela");
        cancela.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelaActionPerformed(evt);
            }
        });

        checkOutTitulo.setText("sin_dados_de_hospede");

        consumoLabel.setText("Consumos");

        saldoInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saldoInputActionPerformed(evt);
            }
        });

        saldoLabel.setText("Saldo");

        jLabel1.setText("Total");

        totalPendente.setDoubleBuffered(true);
        totalPendente.setEnabled(false);

        calcular.setText("Calcular");
        calcular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calcularActionPerformed(evt);
            }
        });

        labelPagamento.setText("Pagamento");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(calcular)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(confirma)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cancela))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel1)
                                    .addComponent(consumoLabel)
                                    .addComponent(saldoLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(consumoInput, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                                    .addComponent(saldoInput, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                                    .addComponent(totalPendente, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)))
                            .addComponent(checkOutTitulo)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelPagamento)
                                .addGap(18, 18, 18)
                                .addComponent(inputPagamento, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)))
                        .addGap(39, 39, 39))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(checkOutTitulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(consumoLabel)
                    .addComponent(consumoInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saldoInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(saldoLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(totalPendente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(calcular)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputPagamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPagamento))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(confirma)
                    .addComponent(cancela))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
    }//GEN-LAST:event_formWindowOpened

    private void confirmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmaActionPerformed

        double pgto;
        ServicosIF sc = ServicosCliente.getInstance(); 
        
        try {

            try {
                pgto = Double.parseDouble(inputPagamento.getText());
            } catch (Exception e) {
                pgto = 0;
            }

            this.reserva.setTotalPago(pgto);
            this.reserva.setTotalHospedagem(reserva.getTotalHospedagem() - pgto + consumos);
            this.reserva.getQuarto().setOcupado(false);
            
            if (sc.editReserva(this.reserva) == Utils.SUCESSO) {
                Hospede h = reserva.getHospede();
                h.setSaldo(reserva.getTotalHospedagem());
                sc.editHospede(h);
                JOptionPane.showMessageDialog(null, "Checkout realizado com sucesso");
            } else {
                JOptionPane.showMessageDialog(null, "Não foi posível realizar o checkout");
            }

        } catch (RemoteException ex) {
            Logger.getLogger(FormCheckOut.class.getName()).log(Level.SEVERE, null, ex);
        }

        this.setVisible(false);
    }//GEN-LAST:event_confirmaActionPerformed

    private void cancelaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelaActionPerformed

        this.setVisible(false);
    }//GEN-LAST:event_cancelaActionPerformed

    private void calcularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calcularActionPerformed

        getSomaValores();
        totalPendente.setText(String.valueOf(total));
        totalPendente.repaint();
    }//GEN-LAST:event_calcularActionPerformed

    private void saldoInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saldoInputActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_saldoInputActionPerformed

    private double getSomaValores() {

        try {
            saldo = Double.parseDouble(this.saldoInput.getText());
            consumos = Double.parseDouble(this.consumoInput.getText());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Deverá inserir valores em ambos campos.");
        }

        total = saldo + consumos;

        return total;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton calcular;
    private javax.swing.JButton cancela;
    private javax.swing.JLabel checkOutTitulo;
    private javax.swing.JButton confirma;
    private javax.swing.JTextField consumoInput;
    private javax.swing.JLabel consumoLabel;
    private javax.swing.JTextField inputPagamento;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel labelPagamento;
    private javax.swing.JTextField saldoInput;
    private javax.swing.JLabel saldoLabel;
    private javax.swing.JTextField totalPendente;
    // End of variables declaration//GEN-END:variables
}
